<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="city in citylists.hotCity" :key="city.id" @click="hotcityclick(city.city)">
            <div class="button">{{city.city}}</div>
          </div>
          
        </div>
      </div>
      <div class="area" 
            v-for="citys in citylists.allCity" 
            :key="citys.id"
            :ref="citys.indexes"
      >
        <div class="title border-topbottom">{{citys.indexes}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="(city,index) in citys.cityList" :key="index" @click="hotcityclick(city.chinese)">
           {{city.chinese}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from "better-scroll";
import {mapState, mapMutations} from 'vuex'
export default {
  props:['citylists','letter'],
  methods:{
    hotcityclick(city){
      // this.$store.dispatch('changecity',city)
      this.changecity(city);
      this.$router.push({path: '/'});
    },
    ...mapMutations(['changecity'])
  },
  computed:{
    ...mapState({
      //'city'相当于function(state){return state.city}
      currentCity:'city'
    })
  },
  mounted() {
    if (!this.scroll) {
      this.scroll = new Bscroll(this.$refs.wrapper, {
        click: true
      });
    } else {
      this.scroll.refresh();
    }
  },
  watch:{
    letter(){
      if(this.letter){
        const element=this.$refs[this.letter][0];
        this.scroll.scrollToElement(element)
      }
      
    }
  }
};
</script>

<style scoped>
.border-bottom::before {
  border-color: #666;
}

.list {
  overflow: hidden;
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
}

.title {
  line-height: 0.54rem;
  background-color: #eee;
  padding-left: 0.2rem;
  color: #666;
}

.button-list {
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
  overflow: hidden;
}

.button-wrapper {
  float: left;
  width: 33.3%;
}

.button {
  margin: 0.1rem;
  border-radius: 0.06rem;
  border: 0.02rem solid;
  padding: 0.1rem 0;
  text-align: center;
}

.item {
  line-height: 0.76rem;
  padding-left: 0.2rem;
}
</style>
